package online.longzipeng.householdbillssystem.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import online.longzipeng.householdbillssystem.common.utils.ExceptionUtils;
import online.longzipeng.householdbillssystem.config.OssConfig;
import online.longzipeng.householdbillssystem.service.FileService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.UUID;

/**
 * @Author: lzp
 * @Date: 2020/10/28 19:48
 * @Description:
 */
@Service
public class FileServiceImpl implements FileService {
    @Autowired
    private OssConfig ossConfig;
    private static final String fileUrlPre = "https://house-hold-bills-sys.oss-cn-shenzhen.aliyuncs.com/";

    @Override
    public String upload(InputStream inputStream, String module, String originalFilename) {
        // 获得oss 服务器的信息
        String endpoint = ossConfig.getEndpoint();
        String keyid = ossConfig.getKeyid();
        String keysecret = ossConfig.getKeysecret();
        String bucketname = ossConfig.getBucketname();

        //判断oss实例是否存在：如果不存在则创建，如果存在则获取
        OSS ossClient = new OSSClientBuilder().build(endpoint, keyid, keysecret);
        if (!ossClient.doesBucketExist(bucketname)) {
            //创建bucket
            ossClient.createBucket(bucketname);
            //设置oss实例的访问权限：公共读
            ossClient.setBucketAcl(bucketname, CannedAccessControlList.PublicRead);
        }

        //构建日期路径：avatar/2020/08/11/文件名
        String folder = new DateTime().toString("yyyy/MM/dd");

        //文件名：uuid.扩展名
        String fileName = UUID.randomUUID().toString();
        String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String key = module + "/" + folder + "/" + fileName + fileExtension;

        //文件上传至阿里云
        ossClient.putObject(ossConfig.getBucketname(), key, inputStream);

        // 关闭OSSClient。
        ossClient.shutdown();

        //返回url地址
        return "https://" + bucketname + "." + endpoint + "/" + key;

    }

    @Override
    public void deleteByFileUrl(String fileUrl) {
        ExceptionUtils.isNotNull(fileUrl,"文件路径不能为null");
        // 去除fileUrl的前缀
        if (fileUrl.contains(fileUrlPre)) {
            fileUrl = fileUrl.substring(fileUrlPre.length());
        }
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(ossConfig.getEndpoint(), ossConfig.getKeyid(), ossConfig.getKeysecret());
        ossClient.deleteObject(ossConfig.getBucketname(), fileUrl);
        // 关闭OSSClient。
        ossClient.shutdown();
    }

}
